
/**
 * href APP分享链接
 * title 分享链接标题
 * summary 分享链接内容
 */
export const appShare = (href, title, summary) => {
	uni.share({
		provider: "weixin",
		scene: "WXSceneSession",
		type: 0,
		href: href,
		title: title,
		summary: summary,
		imageUrl: "https://bjetxgzv.cdn.bspapp.com/VKCEYUGU-uni-app-doc/d8590190-4f28-11eb-b680-7980c8a877b8.png",
		complete: () => {
		},
		fail: (e) => {
			console.log(e)
			console.log(this.base64)
		}
	})
}

export const shareFriend = (base64) => {
	const bitmap = new plus.nativeObj.Bitmap("test")
	bitmap.loadBase64Data(base64, function() {
		const url = "_doc/" + new Date().getTime() + ".png"; // url为时间戳命名方式
		bitmap.save(url, {
			overwrite: true, // 是否覆盖
			// quality: 'quality'  // 图片清晰度
		}, (i) => {
			uni.share({
				provider: "weixin",
				scene: "WXSceneSession",
				type: 2,
				imageUrl: url,
				complete: () => {
					bitmap.clear()
				},
				fail: (e) => {
					console.log(e)
					console.log(this.base64)
				}
			})
		}, (e) => {
			uni.showToast({
				title: '图片保存失败',
				icon: 'none'
			})
			bitmap.clear()
		});
	}, (e) => {
		uni.showToast({
			title: '图片保存失败',
			icon: 'none'
		})
		bitmap.clear()
	})
}

export const shareMoment = (base64) => {
	const bitmap = new plus.nativeObj.Bitmap("test")
	bitmap.loadBase64Data(base64, function() {
		const url = "_doc/" + new Date().getTime() + ".png"; // url为时间戳命名方式
		console.log('saveHeadImgFile', url)
		bitmap.save(url, {
			overwrite: true, // 是否覆盖
			// quality: 'quality'  // 图片清晰度
		}, (i) => {
			uni.share({
				provider: "weixin",
				scene: "WXSceneTimeline",
				type: 2,
				imageUrl: url,
				complete: () => {
					bitmap.clear()
				},
				fail: (e) => {
					console.log(e)
				}
			})
		}, (e) => {
			uni.showToast({
				title: '图片保存失败',
				icon: 'none'
			})
			bitmap.clear()
		});
	}, (e) => {
		uni.showToast({
			title: '图片保存失败',
			icon: 'none'
		})
		bitmap.clear()
	})
}

export const judgeIosPermissionPhotoLibrary = (base64) => {
		if(uni.getSystemInfoSync().platform == 'ios') {
			var PHPhotoLibrary = plus.ios.import("PHPhotoLibrary");
			var authStatus = PHPhotoLibrary.authorizationStatus();
			if (authStatus == 3) {
				saveAlbum(base64)
			} else {
				uni.showToast({
					title:'请先开启相册权限！',
					icon:'none',
					duration:3000
				})
			}
			plus.ios.deleteObject(PHPhotoLibrary);
		} else {
			plus.android.requestPermissions(['android.permission.WRITE_EXTERNAL_STORAGE'], onSuccess, onError)
			function onSuccess(res) {
				const grantedList = res.granted
				const deniedList = res.deniedPresent
				const deniedAlwaysList = res.deniedAlways
				console.log(res)
				// android.permission.WRITE_EXTERNAL_STORAGE
				// android.permission.READ_EXTERNAL_STORAGE
				if(grantedList.includes("android.permission.WRITE_EXTERNAL_STORAGE")) {
					saveAlbum(base64)
				} else {
					uni.showToast({
						title:'请先开启相册权限！',
						icon:'none',
						duration:3000
					})
				}
			}
			function onError(err) {
				uni.showToast({
					title:err,
					icon:'none',
					duration:3000
				})
			}
		}
		
}

export const saveAlbum = (base64) => {
	const bitmap = new plus.nativeObj.Bitmap("test");
	bitmap.loadBase64Data(base64, function() {
	    const url = "_doc/" + new Date().getTime() + ".png";  // url为时间戳命名方式
	    console.log('saveHeadImgFile', url)
	    bitmap.save(url, {
	        overwrite: true,  // 是否覆盖
	        // quality: 'quality'  // 图片清晰度
	    }, (i) => {
	        uni.saveImageToPhotosAlbum({
	            filePath: url,
	            success: function() {
	                uni.showToast({
	                    title: '图片保存成功',
	                    icon: 'none'
	                })
	                bitmap.clear()
	            }
	        });
	    }, (e) => {
	        uni.showToast({
	            title: '图片保存失败',
	            icon: 'none'
	        })
	        bitmap.clear()
	    });
	}, (e) => {
	    uni.showToast({
	        title: '图片保存失败',
	        icon: 'none'
	    })
	    bitmap.clear()
	});
}